//app.js
import { http } from '/server/http.server';
import { httpRouter } from '/server/http.router';
import { coordinate } from '/server/coordinate';
import { storage } from 'config'
App({
  data:{
    systemInfo:{},
    location:{},
    userInfo: {},
    loadStatus:false,
    loginStatus:false,
    systemStatus:false,
    locationStatus:false
  },
  onLaunch: function() {
   
    this.getSetting();
    this.getLocation();
    this.wxLogin();
    this.getSystemInfo();
  },
  getSetting:function(){
    let that = this;
    wx.getSetting({
      success(res) {
        console.log(res.authSetting['scope.userInfo']);
        if (!res.authSetting['scope.userInfo']) {
          wx.authorize({
            scope: 'scope.userInfo',
            success() {
              console.log("success");
              that.wxLogin();
            },
            fail(res){
              console.log(res);
              console.log("fail");
            }
          })
        }
        console.log(res.authSetting['scope.userLocation']);
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              that.getLocation();
            }
          })
        }
      }
    })
  },
  wxLogin:function(){
    let that = this;
    wx.login({
      success: function (res) {
        that.getAppId(res.code);
      }
    });
  },
  getAppId: function (code) {
    let that = this;
    let url = httpRouter.getAppIdUrl + '?code=' + code;
    http.post(url, {}).subscribe({
      next: res => {
        let openid = res.data.openid;
        that.getWxUserInfo(openid);
      },
      error: err => console.log(err)
    })
  },
  getLocation:function(){
    let that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        let gcj = coordinate.wgs84togcj02(res.longitude, res.latitude);
        that.data.location.lat = gcj[1];
        that.data.location.lng = gcj[0];
        that.getCity(gcj[0], gcj[1]);
        that.data.locationStatus = true;
        if (that.data.loginStatus && that.data.systemStatus && that.data.locationStatus) {
          that.data.loadStatus = true;
        }
      }
    })
  },
  getCity: function (lng, lat){
    let that = this;
    let url = httpRouter.locations(lng, lat);
    http.get(url).subscribe({
      next: res => {
        that.globalData.city = res.data.Content.CurrentCity
      },
      error: err => console.log(err)
    })
  },
  /**
   * 获取设备信息
   */
  getSystemInfo: function () {
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.data.systemInfo = res;
        that.data.systemStatus = true;
        if (that.data.loginStatus && that.data.systemStatus && that.data.locationStatus) {
          that.data.loadStatus = true;
        }
      }
    })
  },
  
  getWxUserInfo: function(appid) {
    //调用登录接口
    let that = this;
    wx.getUserInfo({
        withCredentials: false,
          success: function(res) {
            that.userLogin(appid, JSON.parse(res.rawData))
      }
    })
    
  },
  userLogin(appid,user){
    let that = this;
    http.post(httpRouter.loginUrl, {
      'ThirdLoginId': appid,
      'NickName': user.nickName,
      'Sex': user.gender ? 'm' : 'w',
      'Image': user.avatarUrl
    }).subscribe({
      next: res => {
        that.data.userInfo = res.data.Content;
        that.globalData.userInfo = res.data.Content;
        that.data.loginStatus = true;
        if (that.data.loginStatus && that.data.systemStatus && that.data.locationStatus) {
          that.data.loadStatus = true;
        }
      },
      error: err => console.log(err)
    })
  },
 
  globalData: {
    userInfo: {},
    systemInfo: {},
    city: {},
  },
  getDistance:function(lat2, lng2) {
    let lat1 = this.data.location.lat;
    let lng1 = this.data.location.lng;
    if ((Math.abs(lat1) > 90) || (Math.abs(lat2) > 90)) {
      return;
    }
    if ((Math.abs(lng1) > 180) || (Math.abs(lng2) > 180)) {
      return;
    }
    var radLat1 = this.rad(lat1);
    var radLat2 = this.rad(lat2);
    var a = radLat1 - radLat2;
    var b = this.rad(lng1) - this.rad(lng2);
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
      Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    s = s * 6378.137;// EARTH_RADIUS;
    s = Math.round(s * 10000) / 10000;
    s = s.toFixed(0);
    return s;
  },
  rad:function (d) {
    return d * Math.PI / 180.0;
  }
})
